// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import $ from 'jquery'
//判断设备
import global from './assets/js/global'
import 'lib-flexible/flexible.js'
import moment from 'moment'
import {
  getStore
} from '../src/config/mUtils'

require("promise.prototype.finally").shim();

Vue.filter('moment', function (value, formatString) {
  formatString = formatString || 'YYYY-MM-DD HH:mm:ss';
  return moment(value * 1000).format(formatString);
});

//axios 发送post 请求需要qs 转为字符串
import qs from "qs"


//ceshi

Vue.prototype.qs = qs;
import axios from 'axios'
// 请求头
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'
//接口url
axios.defaults.baseURL = 'http://api.menpuwang.com/api/';
Vue.prototype.$axios = axios
//配置跨域访问的选项
// axios.defaults.withCredentials = true
Vue.config.productionTip = false
Vue.prototype.global = global

// vant按需引入
import {
  Tabbar,
  Area,
  TreeSelect,
  Popup,
  List,
  Picker,
  Swipe,
  SwipeItem,
  TabbarItem,
  Checkbox,
  CheckboxGroup,
  Cell,
  CellGroup,
  NavBar,
  Panel,
  Tab,
  Tabs,
  Field,
  Button,
  Dialog,
  Collapse,
  CollapseItem,
  Loading,
  RadioGroup,
  Radio,
  DropdownMenu,
  DropdownItem,
  Overlay,
  IndexBar,
  IndexAnchor,
  Uploader,
  Icon
} from 'vant';

Vue.use(Cell).use(Icon).use(TreeSelect).use(CellGroup).use(NavBar).use(Panel).use(Tab).use(Tabs).use(Field).use(Button).use(Dialog).use(IndexBar).use(IndexAnchor);
Vue.use(Popup).use(Area).use(Tabbar).use(Swipe).use(SwipeItem).use(TabbarItem).use(Checkbox).use(CheckboxGroup).use(Uploader)
Vue.use(Picker).use(List).use(Popup).use(Area).use(Tabbar).use(Collapse).use(CollapseItem).use(Loading).use(RadioGroup).use(Radio).use(DropdownMenu).use(DropdownItem).use(Overlay);


/* eslint-disable no-new */
// 地图

// import AMap from 'vue-amap';
// Vue.use(AMap);
// // 初始化vue-amap
// AMap.initAMapApiLoader({
//   // 高德key
//   key: '862d61a9778339c545c128b1fe60e8c5',
//   // 插件集合 （插件按需引入）
//   plugin: ['AMap.Geolocation']
// });

router.beforeEach((to, from, next) => {
  if ( to.meta.requireAuth ) {
    if ( getStore("token") || getStore("userName") ) {
      next();
    } else {
      next({
        path: "/login"
      });
    }
  } else {
    next();
  }

  if ( to.fullPath === "/user" ) {
    // console.log(from.fullPath)
    if ( !getStore("token") || !getStore("userName") ) {
      next({
        path: "/login"
      });
    } else {
      next();
    }
  }
});

//判断APP或者浏览器访问
if ( global.isApp ) {
  window.apiready = function () {
    new Vue({
      el: '#app',
      router,
      components: {
        App
      },
      template: '<App/>'
    })
  }
} else {
  new Vue({
    el: '#app',
    router,
    components: {
      App
    },
    template: '<App/>'
  })
}
